#!/bin/bash

set -o errexit

test_default_replicas_service() {
	kpatch_delete_field pxc $1 "/spec/haproxy/exposeReplicas" && sleep 1
	compare_kubectl "service/$1-haproxy-replicas"
}

test_disable_replicas_service() {
	kpatch_set_field pxc $1 "/spec/haproxy/exposeReplicas" {} && sleep 1
	kpatch_set_field pxc $1 "/spec/haproxy/exposeReplicas/enabled" false && sleep 1
	wait_for_delete "svc/$1-haproxy-replicas"
	(kubectl_bin get svc "$1-haproxy-replicas" || :) 2>&1 | grep -e "not found$" >/dev/null
}

test_enable_replicas_service() {
	kpatch_set_field pxc $1 "/spec/haproxy/exposeReplicas/enabled" true && sleep 1
	compare_kubectl "service/$1-haproxy-replicas"
}

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions

set_debug

fail_pod() {
	local pod=$1

	yq eval '
		.metadata.name = "chaos-pod-failure" |
		del(.spec.selector.pods.test-namespace) |
		.spec.selector.pods.'$namespace'[0] = "'$pod'"' $conf_dir/chaos-pod-failure.yml \
		| kubectl apply -f -

	sleep 20

}

check_haproxy_writer() {
	for i in $(seq 0 2); do
		local haproxy_pod_ip=$(kubectl_bin get pods $cluster-haproxy-$i -o jsonpath='{.status.podIP}')
		run_mysql "SHOW VARIABLES LIKE 'server_id'" "-h $haproxy_pod_ip -uroot -proot_password" >"/$tmp_dir/server_id_$i.sql"
	done
	for i in $(seq 0 1); do
		diff -u "/$tmp_dir/server_id_$i.sql" "/$tmp_dir/server_id_$((i + 1)).sql"
	done
}

main() {
	create_infra $namespace
	deploy_chaos_mesh $namespace

	if version_gt "1.24"; then
		apply_config "$test_dir/conf/container-rc.yaml"
	elif version_gt "1.19" && [ $EKS -ne 1 ]; then
		apply_config "$test_dir/conf/container-rc.yaml"
	else
		apply_config "$test_dir/conf/docker-rc.yaml"
	fi

	desc 'create first PXC cluster with HAProxy'
	cluster="haproxy"
	spinup_pxc "$cluster" "$test_dir/conf/$cluster.yml" 3 10

	desc 'checking all haproxy pods point to the same writer'
	wait_for_running "$cluster-pxc" 3
	wait_cluster_consistency "$cluster" 3 3
	check_haproxy_writer

	# Temporarily skipping this check
	#desc 'check for passwords leak'
	#check_passwords_leak

	desc 'delete active writer and checking all haproxy pods still point to the same writer'
	desc 'fail pxc-pod-0 pod for 60s'
	fail_pod $cluster-pxc-0
	kubectl_bin get pods
	check_haproxy_writer
	wait_for_running "$cluster-pxc" 3

	desc 'check advanced options are enabled in haproxy statefulset'
	compare_kubectl pdb/$cluster-haproxy
	compare_kubectl statefulset/$cluster-haproxy

	desc 'default haproxy-replicas service'
	test_default_replicas_service $cluster

	desc 'disable haproxy-replicas service'
	test_disable_replicas_service $cluster

	desc 'enable haproxy-replicas service'
	sleep 30
	test_enable_replicas_service $cluster

	wait_for_running "$cluster-haproxy" 3

	kubectl_bin exec haproxy-haproxy-0 -c haproxy -it -- bash -c 'echo "show info" | socat stdio unix-connect:/etc/haproxy/pxc/haproxy.sock' \
		| grep "Maxconn:" >"$tmp_dir"/haproxy_maxconn.txt
	diff --strip-trailing-cr "$tmp_dir"/haproxy_maxconn.txt "$test_dir"/compare/haproxy_maxconn.txt

	apply_config "$test_dir/conf/config-secret-haproxy.yaml"
	wait_cluster_consistency "$cluster" 3 3
	compare_kubectl statefulset/$cluster-haproxy "-secret"
	kubectl_bin exec haproxy-haproxy-0 -c haproxy -it -- bash -c 'echo "show info" | socat stdio unix-connect:/etc/haproxy/pxc/haproxy.sock' \
		| grep "Maxconn:" >"$tmp_dir"/haproxy_maxconn.txt
	diff --strip-trailing-cr "$tmp_dir"/haproxy_maxconn.txt "$test_dir"/compare/haproxy_maxconn-secret.txt

	desc 'clean up'
	destroy_chaos_mesh
	destroy $namespace
	desc "test passed"
}

main
